package com.mj.flowable.examples.config;

import com.mj.flowable.examples.event.TaskEventListener;
import lombok.RequiredArgsConstructor;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEventType;
import org.flowable.common.engine.api.delegate.event.FlowableEventDispatcher;
import org.flowable.spring.SpringProcessEngineConfiguration;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;

@Configuration
@RequiredArgsConstructor
public class ListenerConfig implements ApplicationListener<ContextRefreshedEvent> {

    private final SpringProcessEngineConfiguration configuration;

    private final TaskEventListener taskListener;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        FlowableEventDispatcher dispatcher = configuration.getEventDispatcher();
        dispatcher.addEventListener(taskListener, FlowableEngineEventType.PROCESS_CREATED);
        dispatcher.addEventListener(taskListener, FlowableEngineEventType.TASK_CREATED);
        dispatcher.addEventListener(taskListener, FlowableEngineEventType.TASK_ASSIGNED);

    }


}
